package com.inossem.warehousestorage_inossem.activity.allocation.out_storage;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationDocumentsAdapter;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationDocumentsBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationDocumentsRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;

import java.util.ArrayList;
import java.util.List;

/**
 * 調撥單列表頁
 *
 * @author 詹建宇
 * @time on 2018/3/29 16:27
 * @email jianyu.zhan@inossem.com
 */
public class AllocationOutDocumentsActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private PullableListView listView;

    private List<AllocationDocumentsBean.DocumentBodyBean> list;

    private AllocationDocumentsAdapter adapter;

    private String mCondition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_allocating_listview);

        initView(this);
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) findViewById(R.id.headerTitle);
        listView = (PullableListView) activity.findViewById(R.id.listView);

        listView.setPullDownFlag(false);
        listView.setPullUpFlag(false);
    }

    @Override
    public void initData(Activity activity) {
        title.setText("调拨单列表");
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        mCondition = "";
        list = new ArrayList<>();
        addListData(activity, list, listView);
        getData(activity, "");
    }

    private void addListData(Activity context, List<AllocationDocumentsBean.DocumentBodyBean> mList, ListView listView) {
        adapter = new AllocationDocumentsAdapter(context, mCondition, mList, new SearchListener() {
            @Override
            public void onNoDoubleClickListener(String condition) {
                mCondition = condition;
                getData(context, mCondition);
            }
        });
        listView.setAdapter(adapter);
    }

    private void getData(Activity context, String condition) {
        AllocationDocumentsRequest.request(context, condition, new GsonBeanListener<AllocationDocumentsBean>() {
            @Override
            public void onSuccess(AllocationDocumentsBean bean) {
                list = bean.getBody();
                if (list.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                }
                addListData(context, ActivityUtils.getShowData(list, 0), listView);
            }
        });
    }

    public interface SearchListener {
        void onNoDoubleClickListener(String condition);
    }
}
